package com.me.fragment;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.me.adapter.DBPagerAdapter;
import com.me.adapter.MonthlyPagerAdapter;
import com.me.app.R;
import com.me.util.AppConstant;
import com.me.util.AppUtil;

public class ViewPagerFragment extends Fragment {

	private static CustomViewPager viewPager;
//	private PagerTitleStrip titleStrip;
	private int currentPosition;
	
	private ViewPagerFragment() {
		super();
	}

	public static Fragment newInstance() {
		return new ViewPagerFragment();
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		currentPosition = AppConstant.VIEW_PAGER_FIRST_PAGE;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {

		View v = inflater.inflate(R.layout.fragment_view_pager, container,
				false);

		viewPager = (CustomViewPager) v.findViewById(R.id.pager);
//		titleStrip = (PagerTitleStrip) v.findViewById(R.id.pager_title_strip);
//
//		if (AppUtil.Event.eventType == AppConstant.EVENT_INDEX_PENDING) {
//			titleStrip.setVisibility(View.GONE);
//		} else {
//			titleStrip.setVisibility(View.VISIBLE);
//		}
		
		if (AppUtil.Event.eventType == AppConstant.EVENT_INDEX_PENDING || AppUtil.Event.eventType == AppConstant.EVENT_INDEX_CALENDAR) {
			MonthlyPagerAdapter pagerAdapter = new MonthlyPagerAdapter(
					getFragmentManager());		
			viewPager.setAdapter(pagerAdapter);
		}
		else if (AppUtil.Event.eventType == AppConstant.EVENT_INDEX_TITLE || AppUtil.Event.eventType == AppConstant.EVENT_INDEX_LOCATION) {
			DBPagerAdapter pagerAdapter = new DBPagerAdapter(
					getFragmentManager());		
			viewPager.setAdapter(pagerAdapter);
		}
			
		return v;
	}

	@Override
	public void onStart() {
		super.onStart();

		viewPager.setCurrentItem(currentPosition);
		
		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int position) {
				currentPosition = position;
			}
			
			@Override
			public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
				// TODO Auto-generated method stub		
			}
			
			@Override
			public void onPageScrollStateChanged(int state) {
				// TODO Auto-generated method stub
				
			}
		});
	}
	
	public static CustomViewPager getViewPager() {
		return viewPager;
	}
}
